package com.li.o2oSystem.interceptor.shopadmin;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.li.o2oSystem.entity.PersonInfo;
/**
 * 店家管理系统登录验证拦截器
 * @author Administrator
 *
 */
public class ShopLoginInterceptor extends HandlerInterceptorAdapter{
	//在用户操作前，进行拦截
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler) throws IOException{
		//从session中取出用户信息，
		Object userObj = request.getSession().getAttribute("user");
		if(userObj != null){
			//若信息不为空，则将用户信息转换为PersonInfo实体类
			PersonInfo user = (PersonInfo) userObj;
			//空值判断，确保用户不为空个，且账号可用，并且用户为店家类型
			if(user != null && user.getUserId() != null && user.getUserId() > 0 && user.getEnableStatus() == 1){
				return true;
			}
			
		}
		//若不满足验证，则跳转到登录验证
		PrintWriter out = response.getWriter();
		out.print("<html>");
		out.print("<script>");
		out.print("window.open('"+request.getContextPath()+"/local/login?usertype=2','_self')");
		out.print("</script>");
		out.print("</html>");
		return false;
	} 
}
